package nuist.hanniko.stu.webbackend.controller.api;

import nuist.hanniko.stu.webbackend.dao.mapper.DeviceGroupMapper;
import nuist.hanniko.stu.webbackend.model.DeviceGroup;
import nuist.hanniko.stu.webbackend.model.GroupMemberDevice;
import nuist.hanniko.stu.webbackend.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api/deviceGroup")
public class DeviceGroupController {

    private final DeviceGroupMapper deviceGroupMapper;

    @Autowired
    public DeviceGroupController(DeviceGroupMapper deviceGroupMapper) {
        this.deviceGroupMapper = deviceGroupMapper;
    }

    @PostMapping("/addGroup")
    @ResponseBody
    public ResponseData addGroup(@RequestParam(name = "name") String groupName,
                                 @RequestParam(name = "creator") String creator) {
        ResponseData responseData = new ResponseData();

        long now = System.currentTimeMillis();
        DeviceGroup group = new DeviceGroup(groupName, creator, new Date(now));

        if (deviceGroupMapper.addGroup(group) == 1) {
            responseData.setMessage("SUCCESS");
            responseData.setStatus(true);
        } else {
            responseData.setMessage("FAILURE");
            responseData.setStatus(false);
        }

        return responseData;
    }

    @PostMapping("/addDevice")
    @ResponseBody
    public ResponseData addDevice(@RequestParam(name = "groupName") String groupName,
                                  @RequestParam(name = "deviceId") String deviceId) {
        ResponseData responseData = new ResponseData();

        long now = System.currentTimeMillis();
        GroupMemberDevice memberDevice = new GroupMemberDevice(groupName, deviceId, new Date(now));

        if (deviceGroupMapper.addDevice(memberDevice) == 1) {
            responseData.setMessage("SUCCESS");
            responseData.setStatus(true);
        } else {
            responseData.setMessage("FAILURE");
            responseData.setStatus(false);
        }

        return responseData;
    }

    @PostMapping("/getDevices")
    @ResponseBody
    public ResponseData getAllDevice(@RequestParam(name = "groupName") String groupName) {
        ResponseData responseData = new ResponseData();

        List<GroupMemberDevice> devices = deviceGroupMapper.getAllByGroupId(groupName);
        responseData.setStatus(true);
        responseData.setMessage("SUCCESS");
        responseData.setData(devices);

        return responseData;
    }

    @PostMapping("/getGroups")
    @ResponseBody
    public ResponseData getAllGroup(@RequestParam(name = "creator") String creator) {
        ResponseData responseData = new ResponseData();

        List<DeviceGroup> groups = deviceGroupMapper.getAllGroupByOwner(creator);
        responseData.setStatus(true);
        responseData.setMessage("SUCCESS");
        responseData.setData(groups);

        return responseData;
    }
}
